import { staticRouter } from "@/routers/modules/staticRouter";
import { RouteRecordRaw } from "vue-router";
import { sansanRouter } from "../routers/modules/sansan";

/**
 * @description 递归找出所有面包屑存储到 pinia/vuex 中
 * @param {Array} menuList 所有菜单列表
 * @param {Object} result 输出的结果
 * @param {Array} parent 父级菜单
 * @returns object
 */
export const getAllBreadcrumbList = (menuList: Menu.MenuOptions[], result: { [key: string]: any } = {}, parent = []) => {
	//根据菜单添加面包屑
	result = getBreadcrumbFormMenuList(menuList, parent);

	//添加静态路由面包屑
	function addForRouter(routerData: RouteRecordRaw) {
		if (routerData.children?.length) {
			routerData.children?.forEach(item => {
				addForRouter(item);
			});
		} else if (!routerData.meta?.title || !routerData.path) {
		} else {
			routerData.meta.icon = routerData.meta.icon || "menu";
			result[routerData.path] = [routerData];
		}
	}
	staticRouter.forEach(item => {
		addForRouter(item);
	});

	return result;
};
/**
 * @description 递归找出所有面包屑存储到 pinia/vuex 中
 * @param {Array} menuList 所有菜单列表
 * @param {Object} result 输出的结果
 * @param {Array} parent 父级菜单
 * @returns object
 */
function getBreadcrumbFormMenuList(menuList: Menu.MenuOptions[], result: { [key: string]: any } = {}, parent = []) {
	for (const item of menuList) {
		result[item.path] = [...parent, item];
		if (item.children) getBreadcrumbFormMenuList(item.children, result, result[item.path]);
	}
	return result;
}

/**
 * @description 使用递归，过滤需要缓存的路由（暂时没有使用）
 * @param {Array} menuList 所有菜单列表
 * @param {Array} cacheArr 缓存的路由菜单 name ['**','**']
 * @return array
 * */
export function getKeepAliveRouterName(menuList: Menu.MenuOptions[], keepAliveArr: string[] = []) {
	menuList.forEach(item => {
		item.meta.isKeepAlive && item.name && keepAliveArr.push(item.name);
		item.children?.length && getKeepAliveRouterName(item.children, keepAliveArr);
	});
	return keepAliveArr;
}

/**
 * @description 使用递归，过滤出需要渲染在左侧菜单的列表（剔除 isHide == true 的菜单）
 * @param {Array} menuList 所有菜单列表
 * @return array
 * */
export function getShowMenuList(menuList: Menu.MenuOptions[]) {
	let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList));
	return newMenuList.filter(item => {
		item.children?.length && (item.children = getShowMenuList(item.children));
		return !item.meta?.isHide;
	});
}

/**
 * @description 使用递归处理路由菜单 path，生成一维数组(第一版本地路由鉴权会用到)
 * @param {Array} menuList 所有菜单列表
 * @param {Array} menuPathArr 菜单地址的一维数组 ['**','**']
 * @return array
 */
export function getMenuListPath(menuList: Menu.MenuOptions[], menuPathArr: string[] = []) {
	menuList.forEach((item: Menu.MenuOptions) => {
		typeof item === "object" && item.path && menuPathArr.push(item.path);
		item.children?.length && getMenuListPath(item.children, menuPathArr);
	});
	return menuPathArr;
}

/**
 * 本地业务系统的静态菜单
 * 转换成父节点+子节点数组
 */
export function getSansanLocalMenu() {
	let menuMap: StaticMenuMap = {};
	sansanRouter.forEach(item => {
		//设置所属父节点
		let parent = item.path.substring(0, item.path.lastIndexOf("/"));
		if (!menuMap[parent]) {
			menuMap[parent] = [];
		}
		menuMap[parent].push(item as Menu.MenuOptions);
	});
	console.log("push local router...");
	return menuMap;
}

/**
 * @description 递归修改菜单接口返回
 * @param {Array} menuList 接口菜单列表
 * @param {Object} result 输出的结果
 * @param {Array} parent 父级菜单
 * @returns object
 */
export const convertMenuList = (menuList: any, sansanRouterMap: StaticMenuMap) => {
	return menuList.map((menuData: any) => {
		try {
			menuData.perms = (menuData.perms && JSON.parse(menuData.perms)) || {};
		} catch (e) {
			menuData.perms = { perms: menuData.perms };
		}

		let info = {
			path: menuData.path,
			name: menuData.path?.replaceAll("/", "_") || menuData.id,
			component: menuData.component,
			children: menuData.children ? convertMenuList(menuData.children, sansanRouterMap) : [],
			meta: {
				entitle: menuData.lang,
				title: menuData.menuName,
				icon: menuData.icon,
				id: menuData.id || menuData.path,
				pid: menuData.parentId,
				perms: menuData.perms,
				...menuData.meta
			}
		};

		//將本地路由最近到菜单节点
		let childList = sansanRouterMap[info.path];
		if (childList) {
			childList.forEach(child => {
				info.children.push({
					path: child.path,
					name: child.name,
					component: child.component,
					meta: {
						...child.meta,
						id: child.path,
						pid: info.meta.id
					}
				});
			});
		}
		return info;
	});
};
